﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SocialLearning
{
    public partial class GroupNewForm : Form
    {
        #region Constructor

        /// <summary>
        /// Initializes a new instance of the <see cref="GroupNewForm"/> class.
        /// </summary>
        public GroupNewForm()
        {
            InitializeComponent();
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Creates the group.
        /// </summary>
        /// <param name="name">The name.</param>
        private void CreateGroup(string name)
        {
            try
            {
                UAC_GroupManagement.Instance.Create(name);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion

        #region Private Controls

        /// <summary>
        /// Handles the Click event of the btnAdd control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtCreateName.Text))
                MessageBox.Show("Please enter a group name!", "Information",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
            else
                if (!UAC_GroupManagement.Instance.SearchGroup(txtCreateName.Text))
                {
                    CreateGroup(txtCreateName.Text);
                    MessageBox.Show(string.Format("{0} group was successfully created.", txtCreateName.Text),
                        "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                    MessageBox.Show("Group already exists! Please try again", "Information",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
            txtCreateName.Text = string.Empty;
        }

        /// <summary>
        /// Close the form
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void button1_Click(object sender, EventArgs e)
        {
            Close();
        }

        #endregion
    }
}
